package com.blb.zycommunity.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.zycommunity.entity.*;
import com.blb.zycommunity.service.ISysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 角色信息表 前端控制器
 * </p>
 *
 * @author chenheng
 * @since 2023-11-20
 */
@RestController
@CrossOrigin
public class SysRoleController {

    @Autowired
    private ISysRoleService roleService;

    @GetMapping("/roles")
    public ResponseResult<List<SysRole>> findAllPosts(){
        List<SysRole> allRoles = roleService.findAllRoles();
        return ResponseResult.ok(allRoles);
    }

    @Autowired
    private ISysRoleService sysRoleService;

    /**
     * 模糊查询加分页
     */
    @PostMapping("/sysRole-page")
    public ResponseResult<Page<SysRole>> findLogisticsPage(@RequestBody Rebody selectRole){
        Page<SysRole> sysRolePage = new Page<>(selectRole.getPage(), selectRole.getSize());
        sysRoleService.getSysRolePageVo(sysRolePage,selectRole.getRoleName(),selectRole.getRoleKey(),selectRole.getCreateTime(),selectRole.getStatus());
        return ResponseResult.ok(sysRolePage);
    }

    /**
     * 添加数据
     * @param
     * @return
     */
    @PostMapping("/sysRole")
    public ResponseResult addSysRole(@RequestBody Rebody selectRole){
        LocalDateTime dateTime = LocalDateTime.now();
        selectRole.getSysRole().setCreateTime(dateTime);
        sysRoleService.addSysRole(selectRole.getSysRole(),selectRole.getMenuIds());
        return ResponseResult.ok("添加成功");
    }


    /**
     * 根据角色id查询角色信息
     * @return
     */
    @GetMapping("/SysRoleById/{roleId}")
    public ResponseResult SysRoleById(@PathVariable Long roleId){
        SysRole sysRole = sysRoleService.getById(roleId);
        return ResponseResult.ok(sysRole);
    }


    /**
     * 修改角色状态
     */
    @PutMapping("/sysRoles/roles")
    public ResponseResult UpdateSysRoleStatus(@RequestBody SysRole sysRole){
        sysRoleService.updateById(sysRole);
        return ResponseResult.ok("ok");
    }

    /**
     * 修改数据
     * @param
     * @return
     */
    @PutMapping("/sysRole")
    public ResponseResult UpdateSysRole(@RequestBody Rebody selectRole){
        LocalDateTime dateTime = LocalDateTime.now();
        selectRole.getSysRole().setCreateTime(dateTime);
        sysRoleService.UpdateSysRole(selectRole.getSysRole(),selectRole.getMenuIds());
        return ResponseResult.ok("修改成功");
    }


    /**
     * 删除条数据
     * @param
     * @return
     */
    @DeleteMapping("/Role/deleteById/{ids}")
    public ResponseResult deleteById(@PathVariable Object[] ids){
        sysRoleService.deleteById(ids);
        return ResponseResult.ok("删除成功");
    }


}
